﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using YArchitech.Revit;
using HYElectricKernel;

namespace YArchitech.Plumbing
{
	public class RectLayoutDis
	{
		public RectLayoutDis(RectLayParamers rectLay)
		{
			this.rectParas = rectLay;
		}

		public List<List<XYZ>> CalcInsertPoints()
		{
			List<List<XYZ>> insertPoints = this.GetInsertPoints();
			if (this.IsEmptyPointSet(insertPoints))
			{
				return null;
			}
			return insertPoints;
		}

		private bool IsEmptyPointSet(List<List<XYZ>> pointSet)
		{
			if (pointSet == null || pointSet.Count <= 0)
			{
				return true;
			}
			foreach (List<XYZ> list in pointSet)
			{
				if (list != null && list.Count > 0)
				{
					return false;
				}
			}
			return true;
		}

		private List<List<XYZ>> GetInsertPoints()
		{
			List<List<XYZ>> list = new List<List<XYZ>>();
			if (this.rectParas.XSpace <= 0.0 || this.rectParas.YSpace <= 0.0)
			{
				return null;
			}
			double num = Func.AngleToRadian(this.rectParas.AngleRow);
			XYZ xyz = Geometry.RotateTo(this.rectParas.PointDiagonal, this.rectParas.PointBase, XYZ.BasisZ, -num);
			this.rectParas.XDiagonalPoint = new XYZ(xyz.X, this.rectParas.PointBase.Y, xyz.Z);
			this.rectParas.YDiagonalPoint = new XYZ(this.rectParas.PointBase.X, xyz.Y, xyz.Z);
			this.rectParas.XLength = this.rectParas.PointBase.DistanceTo(this.rectParas.XDiagonalPoint);
			this.rectParas.YLength = this.rectParas.PointBase.DistanceTo(this.rectParas.YDiagonalPoint);
			this.xDist = this.rectParas.XMargin;
			this.yDist = this.rectParas.YMargin;
			if (this.rectParas.DislocationWay == 2)
			{
				list = this.GetInsertPointsOfCol();
			}
			else if (this.rectParas.DislocationWay == 1)
			{
				list = this.GetInsertPointsOfRow();
			}
			else
			{
				list = this.GetInsertPointsOfNone();
			}
			foreach (List<XYZ> list2 in list)
			{
				for (int i = 0; i < list2.Count; i++)
				{
					XYZ pt = list2[i];
					list2[i] = Geometry.RotateTo(pt, this.rectParas.PointBase, XYZ.BasisZ, num);
				}
			}
			return list;
		}

		private List<List<XYZ>> GetInsertPointsOfNone()
		{
			List<List<XYZ>> list = new List<List<XYZ>>();
			if (this.rectParas.LinkLineWay == 2)
			{
				while (this.xDist < this.rectParas.XLength)
				{
					List<XYZ> list2 = new List<XYZ>();
					this.yDist = this.rectParas.YMargin;
					XYZ xyz = this.rectParas.PointBase + this.xDist * (this.rectParas.XDiagonalPoint - this.rectParas.PointBase).Normalize();
					while (this.yDist < this.rectParas.YLength)
					{
						XYZ item = xyz + this.yDist * (this.rectParas.YDiagonalPoint - this.rectParas.PointBase).Normalize();
						this.yDist += this.rectParas.YSpace;
						list2.Add(item);
					}
					this.xDist += this.rectParas.XSpace;
					list.Add(list2);
				}
			}
			else
			{
				while (this.yDist < this.rectParas.YLength)
				{
					List<XYZ> list3 = new List<XYZ>();
					this.xDist = this.rectParas.XMargin;
					XYZ xyz2 = this.rectParas.PointBase + this.yDist * (this.rectParas.YDiagonalPoint - this.rectParas.PointBase).Normalize();
					while (this.xDist < this.rectParas.XLength)
					{
						XYZ item2 = xyz2 + this.xDist * (this.rectParas.XDiagonalPoint - this.rectParas.PointBase).Normalize();
						this.xDist += this.rectParas.XSpace;
						list3.Add(item2);
					}
					this.yDist += this.rectParas.YSpace;
					list.Add(list3);
				}
			}
			return list;
		}

		private List<List<XYZ>> GetInsertPointsOfRow()
		{
			List<List<XYZ>> list = new List<List<XYZ>>();
			int num = 1;
			if (this.rectParas.LinkLineWay == 2)
			{
				while (this.xDist < this.rectParas.XLength)
				{
					List<XYZ> list2 = new List<XYZ>();
					this.yDist = (Func.IsOdd(num) ? this.rectParas.YMargin : (this.rectParas.YMargin + this.rectParas.YSpace));
					XYZ xyz = this.rectParas.PointBase + this.xDist * (this.rectParas.XDiagonalPoint - this.rectParas.PointBase).Normalize();
					while (this.yDist < this.rectParas.YLength)
					{
						XYZ item = xyz + this.yDist * (this.rectParas.YDiagonalPoint - this.rectParas.PointBase).Normalize();
						this.yDist += this.rectParas.YSpace * 2.0;
						list2.Add(item);
					}
					num++;
					this.xDist += this.rectParas.XSpace / 2.0;
					list.Add(list2);
				}
			}
			else
			{
				while (this.yDist < this.rectParas.YLength)
				{
					List<XYZ> list3 = new List<XYZ>();
					this.xDist = (Func.IsOdd(num) ? this.rectParas.XMargin : (this.rectParas.XMargin + this.rectParas.XSpace / 2.0));
					XYZ xyz2 = this.rectParas.PointBase + this.yDist * (this.rectParas.YDiagonalPoint - this.rectParas.PointBase).Normalize();
					while (this.xDist < this.rectParas.XLength)
					{
						XYZ item2 = xyz2 + this.xDist * (this.rectParas.XDiagonalPoint - this.rectParas.PointBase).Normalize();
						this.xDist += this.rectParas.XSpace;
						list3.Add(item2);
					}
					num++;
					this.yDist += this.rectParas.YSpace;
					list.Add(list3);
				}
			}
			return list;
		}

		private List<List<XYZ>> GetInsertPointsOfCol()
		{
			List<List<XYZ>> list = new List<List<XYZ>>();
			int num = 1;
			if (this.rectParas.LinkLineWay == 2)
			{
				while (this.xDist < this.rectParas.XLength)
				{
					List<XYZ> list2 = new List<XYZ>();
					this.yDist = (Func.IsOdd(num) ? this.rectParas.YMargin : (this.rectParas.YMargin + this.rectParas.YSpace / 2.0));
					XYZ xyz = this.rectParas.PointBase + this.xDist * (this.rectParas.XDiagonalPoint - this.rectParas.PointBase).Normalize();
					while (this.yDist < this.rectParas.YLength)
					{
						XYZ item = xyz + this.yDist * (this.rectParas.YDiagonalPoint - this.rectParas.PointBase).Normalize();
						this.yDist += this.rectParas.YSpace;
						list2.Add(item);
					}
					num++;
					this.xDist += this.rectParas.XSpace;
					list.Add(list2);
				}
			}
			else
			{
				while (this.yDist < this.rectParas.YLength)
				{
					List<XYZ> list3 = new List<XYZ>();
					this.xDist = (Func.IsOdd(num) ? this.rectParas.XMargin : (this.rectParas.XMargin + this.rectParas.XSpace));
					XYZ xyz2 = this.rectParas.PointBase + this.yDist * (this.rectParas.YDiagonalPoint - this.rectParas.PointBase).Normalize();
					while (this.xDist < this.rectParas.XLength)
					{
						XYZ item2 = xyz2 + this.xDist * (this.rectParas.XDiagonalPoint - this.rectParas.PointBase).Normalize();
						this.xDist += this.rectParas.XSpace * 2.0;
						list3.Add(item2);
					}
					num++;
					this.yDist += this.rectParas.YSpace / 2.0;
					list.Add(list3);
				}
			}
			return list;
		}

		private RectLayParamers rectParas;

		private double xDist;

		private double yDist;
	}
}
